/**
 * 
 */
package com.gson.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;

/**
 * @作者 Stone
 * @创建日期 2014-6-20 上午11:24:48
 * @版本 V1.0
 * @类说明
 */
public class FileUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String picUrl = "http://api.map.baidu.com/staticimage?center=121.387072,31.168272&width=640&height=320&zoom=17&markers=121.387072,31.168272&markerStyles=l,A";
		// String picUrl =
		// "http://114chahao.duapp.com/static/images/staticimage.png";
		// 目标图片路径
		String tarUrl = "D:/p2.jpg";
		//ioImage(picUrl, tarUrl);
		//savePosImage(picUrl);
		
		String path = "/tmp/jetty-0.0.0.0-18080-root.war-_-any-/webapp/WEB-INF/classes/";
		path = path.substring(0, path.indexOf("/WEB-INF"));
		
	}
	
	/**
	 * 保存位置图片至图片目录
	 * @param srcUrl
	 */
	public static void savePosImage(String srcUrl) {
		ioImage(srcUrl, null);
	}

	/**
	 * 
	 * @param srcUrl 源图片路径
	 * @param tarUrl 源图片路径
	 */
	private static void ioImage(String srcUrl, String tarUrl) {

		try {
			if(StringUtils.isEmpty(tarUrl)) {
				tarUrl = getImagePath();
			}

			// 生成读取图片到内存的输入流
			InputStream finput = HttpUtil.httpRequest(srcUrl);
			
			// 目标图片文件
			File imagefile = new File(tarUrl);
			
			// 生成从内存将图片输出到磁盘的输出流
			FileOutputStream foutput = new FileOutputStream(imagefile);
			BufferedOutputStream bos = new BufferedOutputStream(foutput);

			int b;
			while (true) {
				if (finput.available() < 1024) {
					//
					while ((b = finput.read()) != -1) {

						bos.write(b);
					}
					break;
				} else {

					b = finput.read();
					bos.write(b);
				}
			}
			finput.close();
			bos.close();
			foutput.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static String getImagePath() {
		//String classPath = FileUtil.class.getResource("/").getPath().replaceAll("%20", " ");
		String path = "/tmp/jetty-0.0.0.0-18080-root.war-_-any-/webapp";
		path += "/static/images/staticimage.png";
		return path;
	}
	
	/**
	 * 创建文件
	 * @param file
	 */
	public static File createFile(String filePath) {
		// File fDir = new File(File.separator); //根目录
		// File f = new File(fDir, strFile);
		String[] fileArray = filePath.split("/");
		String filePathTemp = "";
		for (int i = 0; i < fileArray.length - 1; i++) {
			filePathTemp += fileArray[i];
			filePathTemp += File.separator;
		}
		filePathTemp += fileArray[fileArray.length - 1];
		if (filePathTemp.equals("")) {
			filePathTemp = filePath;
		}
		File f = new File(filePathTemp);
		File pf = f.getParentFile();
		if (null != pf && !pf.exists()) {
			pf.mkdirs();
		}
		try {
			f.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
			return f;
		}
		return f;
	}

}
